package com.sdkx.app.fragment.waypoints;

import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.sdkx.app.R;
import com.sdkx.app.ViewModel.WayPointManager;
import com.sdkx.app.model.WayPoint;
import com.sdkx.app.view.ClearEditText;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public abstract class TabWpFragment extends Fragment {

    protected List<WayPoint> getmWPList() {
        return new ArrayList<>();
    }

    protected List<WayPoint> getPageList(int curcout, int pagesize) {

        return null;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
//            if(isVisibleToUser){
//                wpListAdapter.notifyDataSetChanged();
//            }
    }

    private ClearEditText searchtext;
    private ListView wplistView;
    protected WpReferenceAdapter wpListAdapter;
    MutableLiveData<List<WayPoint>> wplivedata;
    protected List<WayPoint> mWPList;     //全部航点列表

    public WpReferenceAdapter getWpListAdapter() {
        return wpListAdapter;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.waypoint_curroute_layout, null);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        searchtext = (ClearEditText) view.findViewById(R.id.clearEditText4);
        wplistView = (ListView) view.findViewById(R.id.routeeditwplist);
        searchtext.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                //getWpListAdapter().setWpList(WayPointManager.filterData(s.toString(),mWPList));
                String keywords = s.toString();
                if (!TextUtils.isEmpty(keywords)) {
                    mWPList = getSearchData(keywords);
                } else {
                    mWPList = getmWPList();
                }
                if (mWPList != null)
                    getWpListAdapter().setWpList(mWPList);
            }
        });
        mWPList = getmWPList();
        Collections.sort(mWPList, new Comparator<WayPoint>() {
            @Override
            public int compare(WayPoint o1, WayPoint o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        wpListAdapter = new WpReferenceAdapter(getContext());
        wpListAdapter.setWpList(mWPList);
        wplistView.setAdapter(wpListAdapter);
        wplistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                WayPoint point = mWPList.get(position);
                onListItemClicked(view, point, position);

            }
        });
        wplistView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if ((firstVisibleItem + visibleItemCount) == totalItemCount) {
                    View lastVisibleItemView = wplistView.getChildAt(wplistView.getChildCount() - 1);
                    if (lastVisibleItemView != null && lastVisibleItemView.getBottom() == wplistView.getHeight()) {
                        if(TextUtils.isEmpty(searchtext.getText().toString())) {
                            List<WayPoint> wayPoints = getPageList(wpListAdapter.getCount(), 30);
                            if (wayPoints != null && wayPoints.size() > 0) {
                                mWPList.addAll(wayPoints);
                                wpListAdapter.setWpList(mWPList);
                            }
                        }

                    }
                }
            }
        });
    }

    protected void onListItemClicked(View view, WayPoint point, int pos) {
    }

    @Override
    public void onDestroyView() {
        //  wplivedata.removeObservers(this);
        super.onDestroyView();
    }

    public static class WpReferenceAdapter extends BaseAdapter {
        Context mContext;

        public WpReferenceAdapter(Context context) {
            mContext = context;
        }

        List<WayPoint> wplist = new ArrayList<>();

        public void setWpList(List<WayPoint> list) {
            wplist = list;
            notifyDataSetChanged();
        }

        public List<WayPoint> getWplist() {
            return wplist;
        }

        @Override
        public int getCount() {
            return wplist.size();
        }

        @Override
        public Object getItem(int position) {
            return wplist.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(mContext, R.layout.wplist_item, null);
                holder = new ViewHolder();
                holder.wpName = convertView.findViewById(R.id.tv_wpname);
                holder.icon = convertView.findViewById(R.id.iv_three_menu_icon);
                convertView.setTag(holder);
                convertView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 80));
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            WayPoint point = wplist.get(position);
            holder.wpName.setText(point.getName());
            return convertView;
        }

        public void deleteitem(int pos) {
            wplist.remove(pos);
            notifyDataSetChanged();
        }

        class ViewHolder {
            TextView wpName;
            ImageView icon;
        }
    }

    protected List<WayPoint> getSearchData(String keywords) {
        List<WayPoint> list = new ArrayList<>();
        return list;
    }
}